<?php

include_once MODELS.'Annalisses/Prepare.php';

class Annalisses {
    
    const MYSQL = "MySQL";
    const POSTGRE = "PostgreSQL";
    const SQLITE = "SQLite";
    const FIREBIRD = "Firebird";
    const ORACLE = "Oracle";
    const ODBC = "ODBC";

    const ERR_DUPLICATE = "1062";
    const ERR_SYNTAX = "1064";
    const ERR_NOT_EXISTS = "1452";
    
    private $SGBD;
    private $prepare;
    static private $connections = array();
    
    public function __construct($SGBD, $prepare) {
        $this->setSGBD($SGBD);
        $this->setPrepare($prepare);
    }
    
    public function setSGBD($SGBD){
        if(file_exists(MODELS.'Annalisses/SGBD/'.$SGBD.'.php')){
            include_once MODELS.'Annalisses/SGBD/'.$SGBD.'.php';
        } 
        $this->SGBD = $SGBD;
    }
    public function getSGBD(){
        return $this->SGBD;
    }
    
    public function setPrepare($prepare){
        $this->prepare = $prepare;
    }
    public function getPrepare(){
        return $this->prepare;
    }
    
    public function getConnection($dbname = null){
        if(!is_null($dbname))
            return self::$connections[$dbname];
        return end(self::$connections);
    }
    
    public function closeConnection($dbname = null){
        if(!is_null($dbname)){
            self::$connections[$dbname] = null;
            unset(self::$connections[$dbname]);
        } else {
            array_pop(self::$connections);
        }
    }
    
    public function connect(){
        $SGBD = $this->getSGBD();
        $prepare = $this->getPrepare();
        $objSGBD = new $SGBD($prepare);
        if(!is_null($prepare->getSpecificInfo())){
            $objSGBD->loadSpecificInfo();
        }
        $con = $objSGBD->getConnection();
        if($con)            
            self::$connections[(string)$prepare->getDBName()] = $con;
        return $con;
    }
    
    public function execute($query, $dbname = null){
        $query = $this->getConnection($dbname)->query($query);
        return $query;
    }
    
    public function fetchAssoc($query){
        $query = $this->fetch($query, PDO::FETCH_ASSOC);
        return $query;
    }
    
    public function fetchObject($query){
        $query = $this->fetch($query, PDO::FETCH_OBJ);
        return $query;
    }
    
    public function fetchArray($query){
        $query = $this->fetch($query, PDO::FETCH_NUM);
        return $query;
    }
    
    public function fetchArrayAssoc($query){
        $query = $this->fetch($query, PDO::FETCH_BOTH);
        return $query;
    }

    public function errorCode(){
        $info = $this->getConnection()->errorInfo(); 
        return $info[1];
    }
    
    public function numRows($query){
        return $query->rowCount();
    }
	  
    public function lastInseredtId($dbname = null){
        return $this->getConnection($dbname)->lastInsertId();
    }

    public function fetch($query, $style){
        if(is_string($query)){
            $query = $this->execute($query);
        }
        $query = $query->fetch($style);
        return $query;
    }
    
    public function DBMS($code){
        $DBMS = array(self::MYSQL, self::POSTGRE, self::SQLITE, self::FIREBIRD, self::ORACLE, self::ODBC);
        if($code < count($DBMS) && $code >= 0)
            return $DBMS[$code];
        return "";
    }

    //BEGIN Transactions functions
    public function beginTransc($dbname = null){
        $con = $this->getConnection($dbname);
        $con->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
        return $con->beginTransaction();
    }
    public function commitTransc($dbname = null){
        $con = $this->getConnection($dbname);
        return $con->commit();	
    }
    public function rollBackTransc($dbname = null){
        $con = $this->getConnection($dbname);
        return $con->rollBack();	
    }

    //END Transactions
}

?>
